package com.mycat.service.auth.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mycat.dao.dto.auth.MenuResourcesDTO;
import com.mycat.dao.dto.auth.ResourcesTreeDTO;
import com.mycat.dao.dto.auth.RoleAuthTreeDTO;
import com.mycat.dao.entity.auth.AuthResource;
import com.mycat.dao.entity.auth.RefRoleAuth;
import com.mycat.dao.mapper.auth.AuthResourceMapper;
import com.mycat.dao.mapper.auth.RefRoleAuthMapper;
import com.mycat.service.auth.AuthResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author roger.
 * @email luojie@yzworld.cn
 * @date 2016/10/9
 */
@Service
public class AuthResourceServiceImpl implements AuthResourceService {

    @Autowired
    private AuthResourceMapper authResourceMapper;

    @Autowired
    private RefRoleAuthMapper refRoleAuthMapper;

    @Override
    public void addResource(AuthResource resource) {
        resource.setCreateDate(new Date());
        resource.setIsDel(0);
        authResourceMapper.insertSelective(resource);
    }

    @Override
    public void delResource(Integer[] ids) {
        for (Integer id : ids){
            authResourceMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void updateResource(AuthResource resource) {
        authResourceMapper.updateByPrimaryKeySelective(resource);
    }

    @Override
    public AuthResource findResource(AuthResource resource) {
        return authResourceMapper.selectAuthResource(resource);
    }

    @Override
    public List<AuthResource> findPermissionByRoleIds(Integer... roleIds) {
        return authResourceMapper.selectPermissionByRoleIds(roleIds);
    }

    @Override
    public List<MenuResourcesDTO> findPageResourcesByRoleIds(Integer parentId, Integer... roleIds) {
        // 获取该角色集合下一级菜单
        Map<String, Object> param = new HashMap<>();
        param.put("parentId", parentId);
        param.put("roleIds", roleIds);
        // 当前parentId的下一级资源
        List<MenuResourcesDTO> resources = authResourceMapper.selectPageResources(param);
        if(null == resources){
            resources = new ArrayList<>();
        }
        findResources(resources,param);
        return resources;
    }

    /**
     * 递归获取子资源
     * @param oneLevelResources 上级资源集合
     *  所有资源
     * @param param 查询参数
     */
    public void findResources(List<MenuResourcesDTO> oneLevelResources,Map<String,Object> param){
        for (MenuResourcesDTO resource : oneLevelResources) {
            param.put("parentId", resource.getId());
            // 当前parentId的所有下一级资源
            List<MenuResourcesDTO> nextLevelResources = authResourceMapper.selectPageResources(param);
            resource.setChildren(nextLevelResources);
            findResources(nextLevelResources, param);
        }
    }

    @Override
    public PageInfo<AuthResource> findResources(Page<AuthResource> page,AuthResource resource) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AuthResource> authResources = authResourceMapper.selectAuthResources(resource);
        PageInfo<AuthResource> pageInfo = new PageInfo<>(authResources);
        return pageInfo;
    }

    @Override
    public List<ResourcesTreeDTO> findResourceTree(final Integer parentId) {
        Map<String,Object> param = new HashMap<String,Object>(){{
            this.put("parentId",parentId);
        }};
        List<ResourcesTreeDTO> resources = authResourceMapper.selectResourceTreeDTOs(param);
        if(null == resources){
            resources = new ArrayList<>();
        }
        findTreeNextResources(resources,param);
        return resources;
    }

    /**
     * 递归获取子资源
     * @param resources 上级资源集合
     *  所有资源
     * @param param 查询参数
     */
    public void findTreeNextResources(List<ResourcesTreeDTO> resources,Map<String,Object> param){
        for (ResourcesTreeDTO resource : resources) {
            param.put("parentId", resource.getId());
            // 当前parentId的所有下一级资源
            List<ResourcesTreeDTO> nextLevelResources = authResourceMapper.selectResourceTreeDTOs(param);
            resource.setChildren(nextLevelResources);
            findTreeNextResources(nextLevelResources, param);
        }
    }

    @Override
    public List<RoleAuthTreeDTO> findAuthTree() {
        List<RoleAuthTreeDTO> authTrees = authResourceMapper.selectRoleAuthTreeDTO(0);
        if(null == authTrees){
            authTrees = new ArrayList<>();
        }
        findAuthTreeNextData(authTrees);
        return authTrees;
    }

    @Override
    public List<RefRoleAuth> findRoleAuth(Integer roleId) {
        return refRoleAuthMapper.selectRoleAuth(roleId);
    }

    /**
     * 递归获取角色授权树的子节点集合
     * @param authTrees 父节点集合
     */
    public void findAuthTreeNextData(List<RoleAuthTreeDTO> authTrees){
        for (RoleAuthTreeDTO authTree : authTrees) {
            // 当前parentId的所有下一级资源
            List<RoleAuthTreeDTO> nextAuthTree = authResourceMapper.selectRoleAuthTreeDTO(authTree.getId());
            authTree.setChildren(nextAuthTree);
            findAuthTreeNextData(nextAuthTree);
        }
    }
}
